package com.action.system;

import java.io.IOException;

import com.action.common.Ajax;
import com.action.common.WEB;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.system.PSWDPk;
import com.service.basic.IModuleService;
import com.service.system.support.PSWDService;
import com.util.ActionLog;
import com.vo.system.PSWDVo;

public class PSWDAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private PSWDService pswdService;
	private PSWDVo  pswdVo;
	private IModuleService moduleService;
	
	public IModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(IModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public void setPswdService(PSWDService pswdService) {
		this.pswdService = pswdService;
	}

	public PSWDVo getPswdVo() {
		return pswdVo;
	}

	public void setPswdVo(PSWDVo pswdVo) {
		this.pswdVo = pswdVo;
	}

	public void ajaxLogin() throws IOException{
		ActionLog.info(this.getClass(), "驗證用戶登錄");
		boolean flag = pswdService.validLogin(pswdVo);
		if(flag){
			ActionLog.info(this.getClass(), "用戶登錄成功，將數據保存到SESSION");
			this.initUser();
			Ajax.sendMsgAjax("success");
		}else{
			ActionLog.info(this.getClass(), "用戶登錄失敗");
			Ajax.sendMsgAjax("error");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void initUser(){
		WEB.getSession().put("username", pswdVo.getUsr());
		WEB.getSession().put("compno", pswdVo.getCompno());
		WEB.getSession().put("modules", moduleService.findAllChildModule("ROOT"));
		PSWDPk  pk  =  new PSWDPk();
		pk.setCompno(pswdVo.getCompno());
		pk.setUsr(pswdVo.getUsr());
		WEB.getSession().put("PSWD", pswdService.findPSWDById(pk));
	}
}
